// packageB/pages/carLeaseDetail/carLeaseDetail.js
let app = getApp();
var applet_park_api_url = app.globalData.applet_park_api_url;
const {
  requestDataparams
} = require('../../../utils/request.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    carNumberList: [],
    currentCarNumber: '',
    contactName:'',
    contactPhone:'',
    timeList: [{
      timer: '一个月',
      id: '1'
    },{
      timer: '两个月',
      id: '2'
    }],
    currentTimer: '',
    currentId: '',
    currentCarInfo: {},
    currentPrice: '',
    currentTimerNum: null,
    isPay:'',
    totalPrice:0,
    time:'',
    id:'',
    orderStatus:'',
    disabled:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.isPay){
      this.setData({
        isPay: options.isPay
      })
    }
    this.setData({
      currentId: options.id
    })
       if(options.isPay == 'pay'){//从我的订单进入此页面
          this.modifyPage()
       }else{
          this.initPage()
       }
       
  },
  // 前往绑定车牌页面
  goAddCar(){
    wx.navigateTo({
      url: '../addplate/addplate',
    })
  },
  getContactName(e){
    
    var that = this;
    var contactName = e.detail.value;
    that.setData({
      contactName: contactName
    })
    that.checkContactName();
  },
  checkContactName(){
    var that = this;
    var contactName = that.data.contactName;
    if(!contactName){
      wx.showToast({
        title: '请输入联系人姓名',
        icon:'none',
      })
    }
  },
  getContactPhone(e){
    
    var that = this;
    var contactPhone = e.detail.value;
    that.setData({
      contactPhone: contactPhone
    })
    that.checkContactPhone();
  },
  checkContactPhone(){
    var that = this;
    var contactPhone = that.data.contactPhone;
    if(!contactPhone){
      wx.showToast({
        title: '请输入联系方式',
        icon:'none',
      })
    }
  },
  // 时长的packer
  bindTimerPickerChange(e){
    // currentTimer
   let obj = this.data.currentCarInfo
   obj.price =  this.data.timeList[e.detail.value].id * this.data.currentPrice

  //  
    this.setData({
      currentTimer: this.data.timeList[e.detail.value].timer,
      currentCarInfo: obj,
      currentTimerNum: this.data.timeList[e.detail.value].id,
      totalPrice: obj.price,
      time: this.data.timeList[e.detail.value].id,
    })
    
  },
  initPage(){
    var that = this;
    var id = that.data.currentId;
    requestDataparams('/api/parkInfo/queryParkInfoById/'+id,{},'POST').then(response=>{
        //
        if(response.data.code==0){
          this.setData({
            currentCarInfo: response.data.data,
            currentPrice: response.data.data.price,
          })
        }
    })
  },
  modifyPage(){//修改
    var that = this;
    var id = that.data.currentId;
    requestDataparams('/api/unifiedOrder/'+id,{},'POST').then(response=>{
        //
        if(response.data.code==0){
          this.setData({
            currentCarInfo: response.data.data.parkInfo,
            currentPrice: response.data.data.parkInfo.price,
            currentCarNumber: response.data.data.orderInfo.carNum,
            currentTimer: response.data.data.orderInfo.endTime,
            contactName: response.data.data.orderInfo.concat,
            contactPhone: response.data.data.orderInfo.phone,
            totalPrice: parseInt(response.data.data.orderInfo.totalMoney)/100,
            time: response.data.data.orderInfo.time,
            id: response.data.data.orderInfo.id,
            orderStatus: response.data.data.orderInfo.orderStatus,
          })
          if(this.data.orderStatus == 2){
            this.setData({
              disabled: 'disabled',
            })
            
          }
        }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  // 保存数据
  saveData(){
        if(!this.data.currentCarNumber){
         wx.showToast({
           title: '请选择车辆',
         })
         return 
        }
        if(!this.data.currentTimer){
          wx.showToast({
            title: '请选择租赁时长',
          })
          return 
        }
        if(!this.data.contactName){
          wx.showToast({
            title: '请输入联系人姓名',
            icon:'none',
          })
          return 
        }
        if(!this.data.contactPhone){
          wx.showToast({
            title: '请输入联系方式',
            icon:'none',
          })
          return 
        }
        if(this.data.isPay == 'pay'){
          this.nextPay();
        }else{
          this.firstPay();
        }        
  },
  firstPay(){
       let that = this;
        var openId = app.globalData.openId || wx.getStorageSync("OPENID");
        var TOKEN = app.globalData.TOKEN || wx.getStorageSync("TOKEN");
        let nickName = app.globalData.nickName|| wx.getStorageSync('nickName');
        var price = parseInt(that.data.totalPrice)*100
        wx.request({
          url: applet_park_api_url + '/api/unifiedOrder/createOrder',
          method: 'POST',
          header: {
            Authorization: TOKEN
          },
          data: {
            orderType:'06', 
            totalMoney: price, //that.data.price
            realityMoney: price, //
            carArea: that.data.currentCarInfo.area,
            time: that.data.currentTimerNum,
            actId:that.data.currentCarInfo.id,
            openId: openId,
            parkNum: that.data.currentCarInfo.carNumber,
            carNum: that.data.currentCarNumber.replace(/\s+/g, ''),
            nickName: nickName,
            concat: that.data.contactName,
            phone: that.data.contactPhone,
          },
          success(res2) {
            if (res2.data.code == 0) {
              var pages = getCurrentPages();
              var prevPage = pages[pages.length - 2]; //上一个页面
              prevPage.getTabData()
              prevPage.getCarData('A区')
              wx.requestPayment({
                timeStamp: res2.data.data.timeStamp,
                nonceStr: res2.data.data.nonceStr,
                package: res2.data.data.prepay_id,
                signType: 'MD5',
                paySign: res2.data.data.sign,
                paySource: 1,
                success(res) {
                  wx.showToast({
                    title: '支付成功',
                  })
    
                  setTimeout(function() {
                    wx.showToast({
                      title: '页面跳转中',
                    })
                    var pages = getCurrentPages();
                    var prevPage = pages[pages.length - 2]; //上一个页面
                    prevPage.getTabData()
                    prevPage.getCarData('A区')
                    wx.navigateBack({
                      delta: 1
                    })
           
                  }, 500);
                },
                fail(res3) {
                  
                }
              })
    
            } else {
              wx.showToast({
                title: '网络异常',
              })
            }
          },
          fail(res) {
            wx.showToast({
              title: res.data,
            })
          }
        })
  },
  nextPay(){
    let that = this;
        var openId = app.globalData.openId || wx.getStorageSync("OPENID");
        var TOKEN = app.globalData.TOKEN || wx.getStorageSync("TOKEN");
        let nickName = app.globalData.nickName|| wx.getStorageSync('nickName');
        var price = parseInt(that.data.totalPrice)*100
        wx.request({
          url: applet_park_api_url + '/api/unifiedOrder/pay',
          method: 'POST',
          header: {
            Authorization: TOKEN
          },
          data: {
            orderType:'06', 
            totalMoney: price, //that.data.price
            realityMoney: price, //
            carArea: that.data.currentCarInfo.area,
            time: that.data.time,
            actId:that.data.currentCarInfo.id,
            id: that.data.id,
            openId: openId,
            parkNum: that.data.currentCarInfo.carNumber,
            carNum: that.data.currentCarNumber.replace(/\s+/g, ''),
            nickName: nickName,
            concat: that.data.contactName,
            phone: that.data.contactPhone,
          },
          success(res2) {
            if (res2.data.code == 0) {
              
              wx.requestPayment({
                timeStamp: res2.data.data.timeStamp,
                nonceStr: res2.data.data.nonceStr,
                package: res2.data.data.prepay_id,
                signType: 'MD5',
                paySign: res2.data.data.sign,
                paySource: 1,
                success(res) {
                  wx.showToast({
                    title: '支付成功',
                  })
                  setTimeout(function() {
                    wx.showToast({
                      title: '页面跳转中',
                    })
                    var pages = getCurrentPages();
                    var prevPage = pages[pages.length - 2]; //上一个页面
                    prevPage.getData()
                  }, 500);
                },
                fail(res3) {
                  
                }
              })
    
            } else {
              wx.showToast({
                title: '网络异常',
              })
            }
          },
          fail(res) {
            wx.showToast({
              title: res.data,
            })
          }
        })
  },
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getCarNum()
  },
// 获取车牌号
getCarNum(){
   let openId = app.globalData.openId || wx.getStorageSync('OPENID')
   let userInfo = wx.getStorageSync("USER_INFO");
   let nickName = app.globalData.nickName || wx.getStorageSync('nickName')
    requestDataparams('/api/parkInfo/queryLicensePlateList',{openId:openId,nickName:nickName }, 'POST','sign').then(response=>{
        // 
        if(response.data.code==0){
          this.setData({
            carNumberList: response.data.data.records
          })
          
        }
   })
},
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})